package Server.ServerCore.Controller;

import Server.ServerCore.Model.AI.BankerAI;
import Server.ServerCore.Model.Player.PlayerInstance;
import Server.ServerCore.Model.vo.CardResultStatusVO;
import Server.ServerCore.Model.vo.CardVO;
import Server.ServerCore.Service.DataPacks.packRespose.*;
import Server.ServerCore.Service.Logic.HandleController;
import Server.ServerCore.Service.Logic.RegisterPack;
import Server.ServerCore.Service.usyns.SingelTableManager;
import javolution.util.FastList;
import org.jboss.netty.buffer.ChannelBuffer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author :石头哥哥<br/>
 *         Project:21Server
 *         Date: 13-4-1
 *         Time: 下午9:59
 *         OverController=26
           保险处理结算
 */
@Service
public class InsueController extends HandleController{

    private static final long serialVersionUID = 2243255867628001438L;

    @Resource
    private SingelTableManager singelTableManager;


    /**
     * 数据分解具体处理的函数
     * 牌局结束，计算数值
     * @param buffer buffer
     * @param player player
     */
    @Override
    public void DispatchPack(ChannelBuffer buffer, PlayerInstance player) throws Exception {

        BankerAI bankerAI =singelTableManager.getBankerAI(player);

        //获取庄家的点数
        int bankerPoints=bankerAI.getPoints();

        //庄家是否是A，是A 发送是位置，Blackjack：0，否，1：是
        //处理玩家保险信息
        boolean isbalck=false;
        insueList_response insueListResponse=new insueList_response();
        FastList<insueChips_response> insueChipsResponses=new FastList<insueChips_response>();
        if (bankerPoints==21&&bankerAI.getCardVOFastList().size()==2){
            isbalck=true;
            bankerAI.setIsbalck(isbalck);
            for (CardResultStatusVO cardResultStatusVO:player.getDeckoOfCardsVOFastList()){
                if(cardResultStatusVO!=null&&cardResultStatusVO.getInsurance()==1){//玩家黑杰克，买保险，2倍保险筹码 flag,注意，玩家保险筹码已加
                    player.setBargainingChip(player.getBargainingChip()+3*cardResultStatusVO.getDoubleChips());
                    insueChips_response insueChipsResponse=new insueChips_response();
                    insueChipsResponse.setCircleUnitID(cardResultStatusVO.getCircleUnitID());
                    insueChipsResponse.setBlackjack((byte) 1);
                    insueChipsResponses.add(insueChipsResponse);
                }
            }
            insueListResponse.setList(insueChipsResponses);
            player.DirectSend(insueListResponse.getJsonData(2,18));
        }else {//
            for (CardResultStatusVO cardResultStatusVO:player.getDeckoOfCardsVOFastList()){
                if(cardResultStatusVO!=null&&cardResultStatusVO.getInsurance()==1){
                    cardResultStatusVO.setDoubleChips(0);
                    player.setBargainingChip(player.getBargainingChip());
                    insueChips_response insueChipsResponse=new insueChips_response();
                    insueChipsResponse.setCircleUnitID(cardResultStatusVO.getCircleUnitID());
                    insueChipsResponse.setBlackjack((byte)0);
                    insueChipsResponses.add(insueChipsResponse);
                }
            }
            insueListResponse.setList(insueChipsResponses);
            player.DirectSend(insueListResponse.getJsonData(2,18));
        }

    }

    /**
     * 通过spring产生处理数据的controller，储存在数组中
     */
    @Override
    public void InitiController() {
        RegisterPack.GAME_CONTROLLERS[26]=this;
    }
}
